﻿Imports TMySQL
Public Class frmProcessList
    Implements ITabForm
    Private _ProcessList As TMyProcessList
    Public Sub New()
        ' この呼び出しは、Windows フォーム デザイナで必要です。
        InitializeComponent()
        ' InitializeComponent() 呼び出しの後で初期化を追加します。
        _ProcessList = New TMyProcessList(frmMain.Connection, False)
        InitFrm()
    End Sub

    Private Sub InitFrm()
        frmMain.GeneralSettings.ApplyAll(Me)
        frmMain.GridSettings.ApplyAll(Me)
    End Sub

    Private Sub cmdRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRefresh.Click
        Cursor.Current = Cursors.WaitCursor
        Try
            ShowProcessList()
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Private Sub ShowProcessList()
        Grid.SuspendLayout()
        Try
            Select Case cboCondition.SelectedIndex
                Case 0
                    _ProcessList.User = txtCondition.Text
                Case 1
                    _ProcessList.Host = txtCondition.Text
                Case 2
                    _ProcessList.DB = txtCondition.Text
            End Select
            _ProcessList.Refresh()
            Grid.DataSource = _ProcessList.DataTable
        Catch ex As Exception
            Grid.ResumeLayout()
        End Try
    End Sub
    Private ReadOnly Property SelectedQueryID() As String
        Get
            If Grid.SelectedRows Is Nothing Then
                Return Nothing
            End If
            If Common.StrToIntDef(Grid.SelectedRows(0).Cells(0).Value, -1) < 0 Then
                Return Nothing
            End If
            Return Grid.SelectedRows(0).Cells(0).Value.ToString
        End Get
    End Property
    Private Sub KillSession(ByVal AQueryID As String, ByVal AQueryOnly As Boolean)
        Dim myQuery As New TMyQuery
        Try
            myQuery.Session = frmMain.Connection
            If AQueryOnly Then
                myQuery.SQL.Append("kill query ").Append(AQueryID)
            Else
                myQuery.SQL.Append("kill connection ").Append(AQueryID)
            End If
            myQuery.Execute()
        Catch ex As Exception
            Throw New Exception(ex.Message)
        Finally
            myQuery.Dispose()
        End Try

    End Sub
    Private Sub mnuKillSession_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuKillSession.Click
        If Me.SelectedQueryID Is Nothing Then
            Return
        End If
        Cursor.Current = Cursors.WaitCursor
        Try
            KillSession(Me.SelectedQueryID, False)
        Catch ex As Exception
            Call MsgBox("エラーが発生しました。" & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub

    Private Sub mnuKillQuery_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuKillQuery.Click
        If Me.SelectedQueryID Is Nothing Then
            Return
        End If
        Cursor.Current = Cursors.WaitCursor
        Try
            KillSession(Me.SelectedQueryID, True)
        Catch ex As Exception
            Call MsgBox("エラーが発生しました。" & vbNewLine & ex.Message, MsgBoxStyle.Exclamation)
        Finally
            Cursor.Current = Cursors.Default
        End Try
    End Sub
End Class